<?php

class ConvertController extends BaseController
{

    protected $db_forum = "vbbed";
    protected $batch_user = 100;
    protected $db_media = "hd_media";
    protected $log;
    protected $category;

    public function __construct()
    {
        $this->log = new Templog();
    }

    public function Index()
    {
        $act = Jinput::get('act', '');
        $param = Jinput::get('p', '');
        $ologs = TempLog::all();
        $logs = array();
        foreach ($ologs as $l)
            $logs[$l->code] = $l->value;
        return View::make('convert.index', compact('logs', 'act', 'param'));
    }

    public function getUserPortal()
    {
        $connect = new Connect($this->db_media);
        $skip = $this->log->getValue('User-Portal');
        $result = $connect->QueryAll("SELECT * FROM table_user ORDER BY userid ASC LIMIT " . $skip . ",500");
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
                $user = new User();
                try {
                    $user->id = $row['userid'];
                    $user->username = $row['username'];
                    $user->password = !empty($row['password']) ? $row['password'] : sha1(strtolower($row['username']) . '123456');
                    $user->salt = $row['password_salt'];
                    $user->email = !empty($row['email']) ? $row['email'] : (Str::slug($row['username']) . $row['userid'] . "@adayroi.net");
                    $user->avatar = $row['avatar'];
                    $user->activated = 1;
                    $user->profile = $row['biography'];
                    $user->money = !empty($row['gold']) ? $row['gold'] : 0;
                    $user->created_at = date('Y-m-d h:i:s', (!empty($row['datejoin']) ? $row['datejoin'] : time()));
                    $user->save();
                    $usergroup = new UserGroup();
                    $usergroup->user_id = $user->id;
                    $usergroup->group_id = Config::get('constants.MEMBER_GROUP');
                    $usergroup->save();
                    // Find the group using the group id
//                $memberGroup = Sentry::findGroupById(Config::get('constants.MEMBER_GROUP'));
                    // Assign the group to the user
//                $user->addGroup($memberGroup);
                } catch (Exception $exx) {

                }
                $skip++;
            }

        } else
            return 'finish';
        $this->log->saveItem(array('code' => 'User-Portal', 'value' => $skip));
        echo $skip;
    }

    public function getTone()
    {
        $connect = new Connect($this->db_media);
        $result = $connect->QueryAll("SELECT * FROM tone");
        while ($row = mysqli_fetch_array($result)) {
            $tone = new Tone();
            $tone->id = $row['id'];
            $tone->title = $row['title'];
            $tone->slug = Str::slug($row['title']);
            $tone->description = $row['descript'];
            $tone->status = $row['status'] == 1 ? "on" : "off";
            $tone->video = $row['video'];
            $tone->save();
        }
        $skip = 'finish';
//        $this->log->saveItem(array('code' => 'tone', 'value' => $skip));
        echo $skip;
    }

    public function getLyrics()
    {
        $connect = new Connect($this->db_media);
        $skip = $this->log->getValue('lyric');
        $result = $connect->QueryAll("SELECT * FROM table_lyric ORDER BY lyricid ASC LIMIT " . $skip . ",500");
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
                $lyric = new Lyric();
                $lyric->id = $row['lyricid'];
                $lyric->title = $row['lyric_name'];
                $lyric->slug = Str::slug($row['lyric_name']);
                $lyric->embed = $row['lyric_embed'];
                $lyric->content = $row['content'];
                $lyric->tone_id = !empty($row['tone']) ? $row['tone'] : 0;
                $lyric->views = $row['views'];
                $lyric->thanks = $row['thanks'];
                $lyric->favourites = $row['favourites'];
                $lyric->user_id = $row['ID_USER'];
                $lyric->created_at = date('Y-m-d h:i:s', $row['postdate']);
                $lyric->is_hot = $row['is_top'];
                $lyric->status = $row['status'] == 1 ? "published" : "submitted";
                $lyric->save();
                $skip++;
            }
        } else
            return 'finish';

        $this->log->saveItem(array('code' => 'lyric', 'value' => $skip));
        echo $skip;
    }

    public function getLessons()
    {
        $connect = new Connect($this->db_media);
        $skip = $this->log->getValue('lessons');
        $result = $connect->QueryAll("SELECT * FROM table_lession ORDER BY le_id ASC LIMIT " . $skip . ",10");

        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
                $lesson = new Lesson();
                $lesson->id = $row['le_id'];
                $lesson->title = $row['le_name'];
                $lesson->slug = Str::slug($row['le_name']);
                if (!empty($row['le_image'])) {
                    $avatar = 'http://hocdan.com/uploads/imglession/' . $row['le_image'];
                    $uploads = Image::uploadFromUrl($avatar, 'lessons', true, false);
                    if ($uploads) {
                        $hash = md5($avatar . time());
                        $media = new Media();
                        $media->hash = $hash;
                        $media->file_path = $uploads['folder'];
                        $media->file_name = $uploads['name'];
                        $media->save();
                        $lesson->thumbnail = $hash;
                    }
                }
                $lesson->youtube = !empty($row['le_embed_yid']) ? $row['le_embed_yid'] : '';
                $lesson->embed = $row['le_embed'];
                $lesson->user_id = $row['ID_USER'];
                $lesson->content = $row['le_content'];
                $lesson->chord = $row['le_chord'];
                $lesson->tab = $row['le_tab'];
                $lesson->views = $row['le_views'];
                $lesson->thanks = $row['le_thanks'];
                $lesson->favourites = $row['le_favs'];
                $lesson->status = $row['le_status'] == 1 ? "published" : "submitted";
                $lesson->created_at = date('Y-m-d h:i:s', $row['le_date']);
                $lesson->save();
                $skip++;
            }
        } else
            return 'finish';
        $this->log->saveItem(array('code' => 'lessons', 'value' => $skip));
        echo $skip;

    }

    public function getQuestion()
    {
        $connect = new Connect($this->db_media);
        $skip = $this->log->getValue('question');
        $result = $connect->QueryAll("SELECT * FROM table_questions ORDER BY q_id ASC LIMIT " . $skip . ",200");
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
                $question = new Question();
                $question->id = $row['q_id'];
                $question->user_id = $row['q_userid'];
                $question->title = $row['q_title'];
                $question->slug = Str::slug($row['q_title']);
                $question->content = $row['q_content'];
                $question->views = $row['q_views'];
                $question->votes = $row['q_vote'];
                $question->is_hot = $row['q_ishot'];
                $question->answers = $row['q_answers'];
                $question->type = $row['type'] == 1 ? 'full' : 'short';
                $question->status = $row['q_status'] == 0 ? "off" : "on";
                $question->category_id = 0;
                $question->created_at = date('Y-m-d h:i:s', $row['q_timestamp']);
                $question->save();
                $skip++;
            }
        } else
            return 'finish';
        $this->log->saveItem(array('code' => 'question', 'value' => $skip));
        echo $skip;
    }

    public function getAnswer()
    {
        $connect = new Connect($this->db_media);
        $skip = $this->log->getValue('answer');
        $result = $connect->QueryAll("SELECT * FROM table_answers ORDER BY a_id ASC LIMIT " . $skip . ",200");
        if (mysqli_num_rows($result)) {
            while ($row = mysqli_fetch_array($result)) {
                $answer = new Answer();
                $answer->id = $row['a_id'];
                $answer->question_id = $row['a_qid'];
                $answer->content = $row['a_content'];
                $answer->votes = $row['a_vote'];
                $answer->user_id = $row['a_userid'];
                $answer->created_at = date('Y-m-d h:i:s', $row['a_timestamp']);
                $answer->save();
                $skip++;
            }
        } else
            return 'finish';
        $this->log->saveItem(array('code' => 'answer', 'value' => $skip));
        echo $skip;
    }

    public function getComments()
    {
        $connect = new Connect($this->db_media);
        $skip = $this->log->getValue('comment');
        $result = $connect->QueryAll('SELECT * FROM table_feeling ORDER BY feelingid ASC LIMIT ' . $skip . ',100');
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
//                try {
                $comment = new Comment();
                $comment->id = $row['feelingid'];
                $comment->video_id = $row['videoid'];
                $comment->lesson_id = $row['lessionid'];
                $comment->lyric_id = $row['lyricid'];
                $comment->parent_id = $row['parentid'];
                $comment->content = $row['content'];
                $comment->user_id = $row['ID_USER'];
                $comment->created_at = date('Y-m-d h:i:s', $row['datepost']);
                $comment->status = 'on';
                $comment->save();
//                } catch (Exception $ex) {
//
//                }
                $skip++;
            }
        } else return 'finish';
        $this->log->saveItem(array('code' => 'comment', 'value' => $skip));
        echo $skip;
    }

    public function getVote()
    {
        $connect = new Connect($this->db_media);
        $skip = $this->log->getValue('vote');
        $result = $connect->QueryAll("SELECT * FROM table_vote where ID_USER != 0 AND (videoid !=0 OR questionid!=0 OR answerid!=0) LIMIT " . $skip . ",100");
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
                $vote = new Vote();
                $vote->video_id = $row['videoid'];
                $vote->question_id = $row['questionid'];
                $vote->answer_id = $row['answerid'];
                $vote->user_id = $row['ID_USER'];
                $vote->save();
                $skip++;
            }
        }else
            return 'finish';
        $this->log->saveItem(array('code' => 'vote', 'value' => $skip));
        echo $skip;
    }
}
